﻿/******************************************************************************
Module:  AsyncResult.cs
Notices: Written by Jeffrey Richter
 
Source: http://msdn.microsoft.com/msdnmag/issues/07/03/ConcurrentAffairs/

         Modified by Matthew Cochran (SetAsCompleted() parameter order changed)

Sample can be found here : http://www.c-sharpcorner.com/uploadfile/rmcochran/multithreadasyncwebservice05262007094719am/multithreadasyncwebservice.aspx?login=true&user=cooldirtyboy

******************************************************************************/

using System;

namespace Jerkymon.Service
{
    public class AsyncResult<TResult> : AsyncResultNoResult
    {
		#region Fields (1)
		// Field set when operation completes
        private TResult m_result = default(TResult);
		#endregion

		#region Constructors (1)
		public AsyncResult(AsyncCallback asyncCallback, Object state) : base(asyncCallback, state) { }
		#endregion

		#region Methods (2)

		new public TResult EndInvoke()
        {
            base.EndInvoke(); // Wait until operation has completed 
            return m_result;  // Return the result (if above didn't throw)
        }

		public void SetAsCompleted(Boolean completedSynchronously, TResult result)
        {
            // Save the asynchronous operation's result
            m_result = result;

            // Tell the base class that the operation completed sucessfully (no exception)
            base.SetAsCompleted(completedSynchronously, null);
        }

		#endregion
    }
}
